﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace eComet
{
    public partial class AllWin : Form
    {
        String[] argv;
        bool debug = false;
        bool scanComplete = false;
        String fileExtensions = ".mp3,.m4a,.mp4,.wav";
        Dictionary<string, string> actions = new Dictionary<string, string>();
        Dictionary<string, List<string>> files = new Dictionary<string, List<string>>();

        public AllWin()
        {
            InitializeComponent();
        }

        public AllWin(String[] argv)
        {
            InitializeComponent();
            this.argv = argv;
        }

        public void cargaParams()
        {
            try
            {
                richTextBox1.Text = "Analizing files ...\n";
                ToxUtils tu = new ToxUtils(argv);
                String paths = tu.getPaths();
                files = tu.getFiles(paths, fileExtensions);
                scanComplete = true;
                tbxArtist.Focus();
            }
            catch (Exception e)
            {
                richTextBox1.Text = e.Message + "\n\n" + richTextBox1.Text;
            }
        }

        private void AllWin_Load(object sender, EventArgs e)
        {
            
            Thread hilo = new Thread(cargaParams);
            hilo.Start();
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        public void hiloCambios()
        {
            while (!scanComplete) ;
            if (chbArtist.Checked)
            {
                actions.Add("/setart", tbxArtist.Text);
            }
            if (chbSong.Checked)
            {
                actions.Add("/setson", tbxSong.Text);
            }
            if (chbAlbum.Checked)
            {
                actions.Add("/setalb", tbxAlbum.Text);
            }
            if (chbGenere.Checked)
            {
                actions.Add("/setgen", tbxGenere.Text);
            }
            ProcessFiles(files, actions);
            ExitApp();
        }

        public bool ProcessFiles(Dictionary<string, List<string>> files, Dictionary<string, string> actions)
        {
            bool exit = false;
            if (files.Count > 0)
            {
                richTextBox1.Text = "Processing files ...\n\n";
                string salida;
                foreach (String tipo in files.Keys)
                {
                    switch (tipo)
                    {
                        default:
                            foreach (String archivo in files[tipo])
                            {
                                salida = ToxUtils.EditID3(archivo, actions, tipo);
                                if (salida != null)
                                    richTextBox1.Text += salida + "\n\n";
                            }
                            break;
                    }
                }
                richTextBox1.Text = "FINISHED !!! \n__________\n\n\n" + richTextBox1.Text;
                ExitApp();
            }
            else
            {
                richTextBox1.Text = "No se encontraron archivos validos en la carpeta selecionada\n";
            }
            return exit;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
            toolStripButton1.Enabled = false;
            Thread hilo = new Thread(hiloCambios);
            hilo.Start();
        }

        public void ExitApp()
        {
            if (!debug)
            {
                Thread.Sleep(1000);
                this.Close();
                this.Dispose();
            }
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        private void tbxArtist_TextChanged(object sender, EventArgs e)
        {
            if (tbxArtist.Text.Equals(String.Empty))
            {
                chbArtist.Checked = false;
            }
            else
            {
                if (!chbArtist.Checked)
                {
                    chbArtist.Checked = true;
                }
            }
        }

        private void tbxSong_TextChanged(object sender, EventArgs e)
        {
            if (tbxSong.Text.Equals(String.Empty))
            {
                chbSong.Checked = false;
            }
            else
            {
                if (!chbSong.Checked)
                {
                    chbSong.Checked = true;
                }
            }
        }

        private void tbxAlbum_TextChanged(object sender, EventArgs e)
        {
            if (tbxAlbum.Text.Equals(String.Empty))
            {
                chbAlbum.Checked = false;
            }
            else
            {
                if (!chbAlbum.Checked)
                {
                    chbAlbum.Checked = true;
                }
            }
        }

        private void tbxGenere_TextChanged(object sender, EventArgs e)
        {
            if (tbxGenere.Text.Equals(String.Empty))
            {
                chbGenere.Checked = false;
            }
            else
            {
                if (!chbGenere.Checked)
                {
                    chbGenere.Checked = true;
                }
            }
        }
    }
}
